// pages/ruzhushops/ruzhushops.js
var app = getApp();
var isPullDown = false; //是否处于下拉状态
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_id:0,//商圈id

    pageIndex: 1,
    isHiddenMes1: true,
    rows: 20, //每页条数
    loadingText: '加载中...', //
    moreData: true, //是否有更多数据
    list: [
      {
        id: 1,
        storeName: '实为记',
        pingfen: 4.9,
        tuijian: 18,
        imgUrl: '../../images/big_avatar.png',
        shops_num: 50,
        store_type: '新概念私房菜'
      },
      {
        id: 2,
        storeName: '艾米1895',
        pingfen: 4.5,
        tuijian: 16,
        imgUrl: '../../images/big_avatar.png',
        shops_num: 35,
        store_type: '电影院'
      },
      {
        id: 3,
        storeName: '妙街集市',
        pingfen: 3.5,
        tuijian: 23,
        imgUrl: '../../images/big_avatar.png',
        shops_num: 66,
        store_type: '家居饰品商铺'
      },

    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var id = parseInt(options.id);
    this.setData({
      shop_id:id
    })

    this.getShopsData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  orderDetail: function (e) {
    console.log(e, "商铺详情");
    wx.navigateTo({
      url: '../shangpudetail/shangpudetail?id=' + e.currentTarget.dataset.id
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageIndex: 1, // 每次触发下拉事件page=0
    })
    //发起请求
    isPullDown = true;
    this.getShopsData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 上拉获取更多数据
    if (this.data.moreData) {
      this.getShopsData();
    }
  },
  /*
  *商铺列表数据
  */
  getShopsData:function(){
    var that = this;
    app.apiPost(app.apiList.shops, {
      content:{
        pageNum: that.data.pageIndex,
        pageSize: that.data.rows,
        parentId: that.data.shop_id
      }
    }, (res) => {
      console.log('商圈--商铺列表',res);

      // 请求成功关闭下拉刷新状态
      if (isPullDown) {
        wx.stopPullDownRefresh();
      }

      if (this.data.pageIndex === 1) {
        let isHiddenMes1 = res.content.list.length > 0 ? false : true;
        that.setData({
          list: res.content.list,
          isHiddenMes1: isHiddenMes1,
          pageIndex: that.data.pageIndex + 1
        })
      } else {
        let newList = this.data.list.concat(res.content.list);
        that.setData({
          list: newList,
          pageIndex: that.data.pageIndex + 1
        })
      }
      // 无更多数据 上拉不再请求数据
      if (res.content.list.length < this.data.rows) {
        this.setData({
          moreData: false
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})